//
//  loginController.m
//  lv678
//
//  Created by apple on 15/6/23.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  登陆页

#import "loginController.h"
#import "AFNetworking.h"
#import "AppDelegate.h"
#import "CommonLoading.h"
#import "SceneryDetailCell.h"

@interface loginController ()<SceneryDetailCellDelegate>
{
    UIView * _registerView;
    UIView * _forgotPswView;
    
}
@property (weak, nonatomic) IBOutlet UITextField *loginAccount;
@property (weak, nonatomic) IBOutlet UITextField *loginPsw;
- (IBAction)forgotPsw:(id)sender;
- (IBAction)loginBtn:(id)sender;
- (IBAction)ToLastPage:(id)sender;


@end

@implementation loginController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark - 初始化视图
- (void)initView{
    _loginAccount.delegate = self;
    _loginPsw.delegate = self;
    
    SceneryDetailCell *detail = [[SceneryDetailCell alloc]init];
    detail.delegate = self;
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"UrlList" ofType:@"plist"];
    NSMutableDictionary *data = [[NSMutableDictionary alloc]initWithContentsOfFile:plistPath];
    url = [data valueForKey:@"memberCenter"];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma makr - 隐藏软键盘
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.view endEditing:YES];
    return  YES;
}

#pragma mark - 判断账号密码长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (_loginAccount == textField) {
        if (range.location>10) {
            return NO;
        }
    }
    if (_loginPsw == textField) {
        if (range.location>19) {
            return NO;
        }
    }
    return  YES;
}

#pragma mark - 忘记密码方法
- (IBAction)forgotPsw:(id)sender {
}

#pragma mark - 动画出现是隐藏导航栏
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = YES;
}

#pragma mark - 登陆按钮方法
- (IBAction)loginBtn:(id)sender {
    UIView *loginView = [[CommonLoading initCommonLoading]loginLoading];
    [self.view addSubview:loginView];
        AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
        manger.responseSerializer = [AFJSONResponseSerializer serializer];
        manger.requestSerializer = [AFJSONRequestSerializer serializer];
        manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

        NSDictionary *login = @{@"serviceName":@"login",@"queryParameters":@{@"loginName":_loginAccount.text,@"password":_loginPsw.text}};
        [manger POST:url parameters:login success:^(AFHTTPRequestOperation *operation, id responseObject) {
            int status = [[responseObject objectForKey:@"status"]intValue];
            NSString *comment = [responseObject valueForKey:@"comment"];
            [loginView removeFromSuperview];
            if (status == 0) {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:comment delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
            }else{
//                AppDelegate *appleDelegate = [[AppDelegate alloc]init];
//                [appleDelegate.loginInfo setObject:_loginAccount.text forKey:@"userName"];
                [self.navigationController pushViewController:self.controller animated:YES];
                }
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            [loginView removeFromSuperview];
            NSLog(@"sss %@",error);
        }];
}

#pragma mark - 实现景点详情代理
- (void)pushViewController:(UIViewController *)viewController sendValue:(NSDictionary *)dict{
    self.controller = viewController;
}

#pragma mark - 返回上一视图
- (IBAction)ToLastPage:(id)sender {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

@end
